package com.onefish.toolkit.functional;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 常用函数式编程
 *
 * @author onefish
 */
public class FunctionalInterface {
    public static void main(String[] args) {
        /*
            1. Lambda 表达式
         */
        new Thread(() -> System.out.println("Hello")).start();
        /*
            2. 函数式接口（Functional Interface）
         */
        //  Predicate<T> 有入参T，有出参（布尔型）
        Predicate<String> isEmpty = s -> s.isEmpty();
        System.out.println(isEmpty.test(""));
        //  Function<T, R> 有入参T，有出参R
        Function<String, Integer> length = String::length;
        System.out.println(length.apply("Java"));
        //  Consumer<T> 有入参T，无无出参（单纯执行逻辑）
        Consumer<String> print = System.out::println;
        print.accept("Hello");
        //  Supplier<T> 无入参，有出参
        Supplier<Double> random = Math::random;
        System.out.println(random.get());
        /*
            3. Stream API
         */
        List<String> list = Arrays.asList("a", "b", "c", "d");
        // 过滤、映射、遍历
        list.stream()
                .filter(s -> s.startsWith("a"))  // 中间操作
                .map(String::toUpperCase)        // 中间操作
                .forEach(System.out::println);   // 终止操作

        // 收集为 List
        List<String> result = list.stream()
                .filter(s -> s.length() > 1)
                .collect(Collectors.toList());
        System.out.println(result);
        // 归约（求和）
        int sum = IntStream.of(1, 2, 3).reduce(0, (a, b) -> a + b);
        System.out.println(sum);
        /*
            4.方法引用（Method Reference）
         */
        // 静态方法引用
        Function<Integer, String> intToString = String::valueOf;
        System.out.println(intToString.apply(1));
        // 实例方法引用
        List<String> names = Arrays.asList("Alice", "Bob");
        names.forEach(System.out::println);
        // 构造方法引用
        Supplier<List<String>> listSupplier = ArrayList::new;
        System.out.println(listSupplier.get());
        /*
            5.Optional 类
         */
        Optional<String> optional = Optional.ofNullable("ssss");
        String name = optional.orElse("default");
        System.out.println(name);
        optional.ifPresent(s -> System.out.println(s.length()));
        // 链式操作
        Optional<String> upperName = optional.map(String::toUpperCase);
        upperName.ifPresent(s -> System.out.println(s.length()));
         /*
            6.并行流（Parallel Stream）
         */
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int Sum = numbers.parallelStream()
                .mapToInt(Integer::intValue)
                .sum();
        System.out.println(Sum);
        /*
            6.Comparator 简化
         */
        List<String> words = Arrays.asList("apple", "banana", "pear");
        words.sort((a, b) -> a.length() - b.length());
        System.out.println(words);
    }
}
